﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bacteria
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : NavigationWindow
    {
        public enum GramTest { GramNegative, GramPositive }
        public enum CellShape { Bacilli, Cocci }
        public enum Bacteria
        {
            E_Coli,
            E_Aerogenes,
            K_Pneumoniae,
            S_Dysenteriae,
            S_Typhimurium,
            P_Vulgaris,
            P_Aeruginosa,
            A_Faecalis,
            S_Aureus,
            L_Lactis,
            M_Luteus,
            C_Xerosis,
            B_Cereus,
            None
        }
        public enum Tests
        {
            H2S_Production,
            NO3_Production,
            Indole_Production,
            MR_Reaction,
            VP_Reaction,
            Citrate_Use,
            Urase_Activity,
            Catalase_Activity,
            Oxidase_Activity,
            Starch_Hydrolysis,
            Lipid_Hydrolysis
        }
        public enum GroupNum { A, B, C, D, E, F, G, None }
        public struct Group
        {
            public GroupNum Number;
            public List<Bacteria> Bacteria;
            public List<Tests> Tests;
        }
        public enum Fermentation { Acid, Acid_Gas, Negative }
        public enum TestResult { Positive, Negative }

        public GramTest gGramTest;
        public CellShape gCellShape;
        public Fermentation gLactose, gDextrose, gSucrose;

        public MainWindow()
        {
            InitializeComponent();
            Navigate(new pageMain(this));
        }

        public Group FindGroup(GramTest gramTest, CellShape shape, Fermentation Lactose, Fermentation Dextrose, Fermentation Sucrose)
        {
            Group Invalid = new Group() { Number = GroupNum.None, Bacteria = null, Tests = null };
            Group group = Invalid;

            switch (gramTest)
            {
                case GramTest.GramNegative:
                    switch (shape)
                    {
                        case CellShape.Cocci:
                            return Invalid;

                        case CellShape.Bacilli:
                            if (Lactose == Fermentation.Acid_Gas & Dextrose == Fermentation.Acid_Gas)
                            {
                                group.Number = GroupNum.A;
                                group.Bacteria = new List<Bacteria>() { Bacteria.E_Coli, Bacteria.E_Aerogenes, Bacteria.K_Pneumoniae };
                                group.Tests = new List<Tests>() { Tests.Indole_Production, Tests.Citrate_Use, Tests.Urase_Activity };
                            }
                            if (Lactose == Fermentation.Negative & Dextrose == Fermentation.Acid)
                            {
                                group.Number = GroupNum.B;
                                group.Bacteria = new List<Bacteria>() { Bacteria.S_Dysenteriae };
                                group.Tests = new List<Tests>() { Tests.H2S_Production, Tests.Citrate_Use };
                            }
                            if (Lactose == Fermentation.Negative && Dextrose != Fermentation.Negative && Sucrose != Fermentation.Negative)
                            {
                                group.Number = GroupNum.C;
                                group.Bacteria = new List<Bacteria>() { Bacteria.S_Typhimurium, Bacteria.P_Vulgaris };
                                group.Tests = new List<Tests>() { Tests.H2S_Production, Tests.Indole_Production, Tests.Urase_Activity };
                            }
                            if (Lactose == Fermentation.Negative && Dextrose == Fermentation.Negative && Sucrose == Fermentation.Negative)
                            {
                                group.Number = GroupNum.D;
                                group.Bacteria = new List<Bacteria>() { Bacteria.P_Aeruginosa, Bacteria.A_Faecalis };
                                group.Tests = new List<Tests>() { Tests.NO3_Production, Tests.Oxidase_Activity, Tests.Lipid_Hydrolysis };
                            }
                            break;

                        default:
                            return Invalid;
                    }
                    break;

                default:
                    return Invalid;

                case GramTest.GramPositive:
                    switch (shape)
                    {
                        case CellShape.Cocci:
                            if (Lactose == Fermentation.Acid && Dextrose == Fermentation.Acid && Sucrose == Fermentation.Acid)
                            {
                                group.Number = GroupNum.E;
                                group.Bacteria = new List<Bacteria>() { Bacteria.S_Aureus, Bacteria.L_Lactis };
                                group.Tests = new List<Tests>() { Tests.NO3_Production, Tests.Catalase_Activity, Tests.Lipid_Hydrolysis };
                            }
                            else if (Lactose == Fermentation.Negative && Dextrose == Fermentation.Negative && Sucrose == Fermentation.Negative)
                            {
                                group.Number = GroupNum.F;
                                group.Bacteria = new List<Bacteria>() { Bacteria.M_Luteus };
                                group.Tests = new List<Tests>() { Tests.Urase_Activity, Tests.Catalase_Activity, Tests.Lipid_Hydrolysis };
                            }

                            else
                                return Invalid;
                            break;

                        case CellShape.Bacilli:
                            if (Lactose == Fermentation.Negative && Dextrose == Fermentation.Acid && Sucrose == Fermentation.Acid)
                            {
                                group.Number = GroupNum.G;
                                group.Bacteria = new List<Bacteria>() { Bacteria.C_Xerosis, Bacteria.B_Cereus };
                                group.Tests = new List<Tests>() { Tests.Starch_Hydrolysis };
                            }
                            else if (Lactose == Fermentation.Negative)
                            {
                                group.Number = GroupNum.G;
                                group.Bacteria = new List<Bacteria>() { Bacteria.C_Xerosis };
                                group.Tests = new List<Tests>() { Tests.Starch_Hydrolysis };
                            }
                            break;

                        default:
                            return Invalid;
                    }
                    break;
            }
            return group;
        }

        public Bacteria FindBacteria(Group group, SortedList<Tests, TestResult> trList)
        {
            Bacteria bacteria = Bacteria.None;

            switch (group.Number)
            {
                case GroupNum.A:
                    if (trList[Tests.Indole_Production] == TestResult.Positive && trList[Tests.Citrate_Use] == TestResult.Negative && trList[Tests.Urase_Activity] == TestResult.Negative)
                        bacteria = Bacteria.E_Coli;
                    if (trList[Tests.Indole_Production] == TestResult.Negative && trList[Tests.Citrate_Use] == TestResult.Positive && trList[Tests.Urase_Activity] == TestResult.Positive)
                        bacteria = Bacteria.K_Pneumoniae;
                    if (trList[Tests.Indole_Production] == TestResult.Negative && trList[Tests.Citrate_Use] == TestResult.Positive && trList[Tests.Urase_Activity] == TestResult.Negative)
                        bacteria = Bacteria.E_Aerogenes;
                    break;

                case GroupNum.B:
                    if (trList[Tests.H2S_Production] == TestResult.Negative && trList[Tests.Citrate_Use] == TestResult.Negative)
                        bacteria = Bacteria.S_Dysenteriae;
                    break;

                case GroupNum.C:
                    if (trList[Tests.H2S_Production] == TestResult.Positive && trList[Tests.Indole_Production] == TestResult.Positive && trList[Tests.Urase_Activity] == TestResult.Positive)
                        bacteria = Bacteria.P_Vulgaris;
                    if (trList[Tests.H2S_Production] == TestResult.Positive && trList[Tests.Indole_Production] == TestResult.Negative && trList[Tests.Urase_Activity] == TestResult.Negative)
                        bacteria = Bacteria.S_Typhimurium;
                    break;

                case GroupNum.D:
                    if (trList[Tests.NO3_Production] == TestResult.Negative && trList[Tests.Oxidase_Activity] == TestResult.Positive && trList[Tests.Lipid_Hydrolysis] == TestResult.Negative)
                        bacteria = Bacteria.A_Faecalis;
                    if (trList[Tests.NO3_Production] == TestResult.Positive && trList[Tests.Oxidase_Activity] == TestResult.Positive && trList[Tests.Lipid_Hydrolysis] == TestResult.Positive)
                        bacteria = Bacteria.P_Aeruginosa;
                    break;

                case GroupNum.E:
                    if (trList[Tests.NO3_Production] == TestResult.Negative && trList[Tests.Catalase_Activity] == TestResult.Positive && trList[Tests.Lipid_Hydrolysis] == TestResult.Positive)
                        bacteria = Bacteria.S_Aureus;
                    if (trList[Tests.NO3_Production] == TestResult.Negative && trList[Tests.Catalase_Activity] == TestResult.Negative && trList[Tests.Lipid_Hydrolysis] == TestResult.Negative)
                        bacteria = Bacteria.L_Lactis;
                    break;

                case GroupNum.F:
                    if (trList[Tests.Urase_Activity] == TestResult.Positive && trList[Tests.Catalase_Activity] == TestResult.Positive && trList[Tests.Lipid_Hydrolysis] == TestResult.Negative)
                        bacteria = Bacteria.M_Luteus;
                    break;

                case GroupNum.G:
                    if (trList[Tests.Starch_Hydrolysis] == TestResult.Negative)
                        bacteria = Bacteria.C_Xerosis;
                    if (trList[Tests.Starch_Hydrolysis] == TestResult.Positive)
                        bacteria = Bacteria.B_Cereus;
                    break;
            }

            return bacteria;
        }
    }
}
